package com.csaba.swing.action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import com.csaba.swing.MainWindow;
import com.csaba.swing.gui.InitStoreDialog;
import com.csaba.swing.gui.util.GUIUtil;
import com.csaba.util.encryption.EncryptedStore;
import com.csaba.util.encryption.EncryptedStoreEvent;
import com.csaba.util.encryption.EncryptedStoreListener;

@SuppressWarnings( "serial" )
public class InitEncryptionAction extends AbstractAction implements EncryptedStoreListener
{
	public InitEncryptionAction()
	{
		super(MainWindow.getString("InitEncryptionAction.menuItem")); //$NON-NLS-1$
		EncryptedStore.getInstance().addEncryptedStoreListener(this);
		setEnabled(!EncryptedStore.getInstance().isInitialized());
		//set icon

		GUIUtil.setMnemonic(this);
	}

	@Override
	public void actionPerformed( final ActionEvent e )
	{
		final InitStoreDialog dialog = new InitStoreDialog(MainWindow.getFrame());
		dialog.setVisible(true);
	}

	@Override
	public void storeUpdated( final EncryptedStoreEvent event )
	{
		if ( event.getEventType() == EncryptedStoreEvent.STORE_DESERIALIZED
				|| event.getEventType() == EncryptedStoreEvent.STORE_OPEN
				|| event.getEventType() == EncryptedStoreEvent.STORE_INITIALIZED )
			setEnabled(false);

	}
}
